/**
 *    Copyright (c) 2021 WANGFEIHU
 *    SSO-PLUS is licensed under Mulan PSL v2.
 *    You can use this software according to the terms and conditions of the Mulan PSL v2.
 *    You may obtain a copy of Mulan PSL v2 at:
 *             http://license.coscl.org.cn/MulanPSL2
 *    THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 *    See the Mulan PSL v2 for more details.
 */
package cn.codinglives.ssoplus.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.saml.metadata.MetadataManager;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Set;

@Controller
@RequestMapping("/saml")
public class SamlController {

	// Logger
	private static final Logger LOG = LoggerFactory
			.getLogger(SamlController.class);

	@Resource
	private MetadataManager metadata;

	@RequestMapping(value = "/idp/selection", method = RequestMethod.GET)
	public ResponseEntity idpSelection(HttpServletRequest request, Model model) {
		Set<String> idps = metadata.getIDPEntityNames();
		LOG.info("idps:{}", idps);
		return ResponseEntity.ok(idps);
	}

	@RequestMapping(value = "/expired", method = RequestMethod.GET)
	public ResponseEntity expired() {
		return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("用户未授权，请重新登录！");
	}

}
